| Conditions | 1 |
| Paths | 1 |
| Total Lines | 98 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 6 | ||
| Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | jQuery( function( $ ) { |
||
| 2 | |||
| 3 | /** |
||
| 4 | * JS meta_box field actions. |
||
| 5 | */ |
||
| 6 | var cmb2MetaBoxField = { |
||
| 7 | |||
| 8 | /** |
||
| 9 | * Initialize meta box fields behavior. |
||
| 10 | */ |
||
| 11 | init: function() { |
||
| 12 | this.show_hide_on_load(); |
||
| 13 | this.field_type_change(); |
||
| 14 | this.field_name_change(); |
||
| 15 | this.display_usage_functions(); |
||
| 16 | }, |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Show / hide field rows based on field_type_select selected value. |
||
| 20 | * |
||
| 21 | * show_hide |
||
| 22 | * |
||
| 23 | * |
||
| 24 | * |
||
| 25 | */ |
||
| 26 | show_hide: function( fieldTypeSelect ) { |
||
| 27 | var fieldTypeVal = $( fieldTypeSelect ).val(); |
||
| 28 | var fieldSet = $( fieldTypeSelect ).closest( '.cmb-field-list' ); |
||
| 29 | $( '.cmb-row.cmb_hide_field', fieldSet ).hide(); |
||
| 30 | $( '.cmb-row.' + fieldTypeVal, fieldSet ).show(); |
||
| 31 | }, |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Use the show_hide() function on each field group's field rows on page load. |
||
| 35 | * |
||
| 36 | * show_hide_on_load |
||
| 37 | * |
||
| 38 | * |
||
| 39 | * |
||
| 40 | */ |
||
| 41 | show_hide_on_load: function() { |
||
| 42 | var fieldTypeSelects = $( '.field_type_select' ); |
||
| 43 | fieldTypeSelects.each( function() { |
||
| 44 | cmb2MetaBoxField.show_hide( this ); |
||
| 45 | } ); |
||
| 46 | |||
| 47 | }, |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Use the show_hide() function on this field group's field rows on field_type_select change. |
||
| 51 | * |
||
| 52 | * field_type_change |
||
| 53 | * |
||
| 54 | * |
||
| 55 | * |
||
| 56 | */ |
||
| 57 | field_type_change: function() { |
||
| 58 | $( '.cmb2-metabox' ).on( 'change', '.field_type_select', function( e ) { |
||
| 59 | var fieldTypeSelect = e.target; |
||
| 60 | cmb2MetaBoxField.show_hide( fieldTypeSelect ); |
||
| 61 | } ); |
||
| 62 | }, |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Update the usage code snippets when field_name changes. |
||
| 66 | * |
||
| 67 | * field_name_change |
||
| 68 | * |
||
| 69 | * |
||
| 70 | * |
||
| 71 | */ |
||
| 72 | field_name_change: function() { |
||
| 73 | $( '.cmb2-metabox' ).on( 'change keyup', '.field_name', function( e ) { |
||
|
|
|||
| 74 | this.display_usage_functions(); |
||
| 75 | }.bind( this ) ); |
||
| 76 | }, |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Convert field name to usage code snippet. |
||
| 80 | * |
||
| 81 | * display_usage_functions |
||
| 82 | * |
||
| 83 | * |
||
| 84 | * |
||
| 85 | */ |
||
| 86 | display_usage_functions: function() { |
||
| 87 | var field_name = $( '.field_name' ); |
||
| 88 | var wrapper = field_name.closest( '.cmb-field-list' ); |
||
| 89 | var get_post_meta = $( '.get_post_meta', wrapper ); |
||
| 90 | var cmbf = $( '.cmbf', wrapper ); |
||
| 91 | get_post_meta.val( 'get_post_meta( get_the_ID(), \'_' + field_name.val().toLowerCase().replace( / /g, '_' ) + '\', true );'); |
||
| 92 | cmbf.val( 'cmbf( get_the_ID(), \'_' + field_name.val().toLowerCase().replace( / /g, '_' ) + '\' );'); |
||
| 93 | } |
||
| 94 | }; |
||
| 95 | |||
| 96 | cmb2MetaBoxField.init(); |
||
| 97 | |||
| 98 | } ); |
||
| 99 |
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.